function Animal(name){
	// 属性
	this.name = name || "Animal"
	
	//实例方法
	this.sleep = function(){
		console.log(this.name + "在睡觉")
	}
}

// 原型
Animal.prototype.eat = function(food){
	console.log(this.name + "在吃" + food)
}

function Cat(){
	this.age = 10
}
Cat.prototype = new Animal()
Cat.prototype.name = "cat"

// 测试
var cat = new Cat()
console.log(cat.name)	//cat
console.log(cat.age) //10
cat.eat("fish")
cat.sleep()
console.log(cat instanceof Animal) //true
console.log(cat instanceof Cat) //true
console.log(cat.constructor == Animal) //true
console.log(cat.constructor == Cat) //false

// 构造继承

function Dog(name){
	Animal.call(this,name)
}

// 测试
var dog = new Dog("wangwang")
console.log(dog.name)	//wangwang
dog.eat("rou")	//Uncaught TypeError: dog.eat is not a function
dog.sleep() //wangwang在睡觉
console.log(dog instanceof Animal) //false
console.log(dog instanceof Dog) //true
console.log(dog.constructor == Animal) //false
console.log(dog.constructor == Dog) //true


// 寄生继承
function Pig(name){
	this.name = name //这里无法继承父类属性，需要重新定义
}

//创建寄生类实例
function superObj(proto) {
    function Super() {}
    Super.prototype = proto
    return new Super()
}

//将寄生类实例作为子类的原型
Pig.prototype = superObj(Animal.prototype)

//测试
var pig = new Pig("佩奇")
console.log(pig.name)	//佩奇
pig.eat("剩饭")	//佩奇在吃剩饭
pig.sleep() //pig.sleep is not a function
console.log(pig instanceof Animal) //true
console.log(pig instanceof Pig) //true
console.log(pig.constructor == Animal) //true
console.log(pig.constructor == Pig) //false


function Cat(name){
	Animal.call(this,name);
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

// 测试
var cat = new Cat("tom")
console.log(cat.name)	//tom
cat.sleep()	//tom在睡觉
console.log(cat instanceof Animal) // true
console.log(cat instanceof Cat) // true
console.log(pig.constructor == Animal) //false	由于重新将Cat赋值给了constructor
console.log(pig.constructor == Cat) //true


function Pig(name){
	Animal.call(this,name);
	Object1.call(this)
	Object2.call(this)
}

//将寄生类实例作为子类的原型
Pig.prototype = Object.create(Animal.prototype)

//Object.assign 会把其他原型上的属性和方法拷贝到Pig原型上
//只是原型方法的拷贝，而实例原型还是Animal
Object.assign(Pig.prototype,
	Object1.prototype,
	Object2.prototype,
	{a:1,b:2},
)

//测试
var pig = new Pig("佩奇")
console.log(pig.name)	//佩奇
pig.eat("剩饭")	//佩奇在吃剩饭
pig.sleep() //佩奇在睡觉
console.log(pig instanceof Animal) //true
console.log(pig instanceof Pig) //true
console.log(pig instanceof Object1) //false
console.log(pig.constructor == Animal) //true
console.log(pig.constructor == Pig) //false
console.log(pig.constructor == Object1) //false


var myAjax = (url,type="GET",data={})=>{
	
	let params = (d) => {
		let arr = []
		for(let prop in d){
			arr.push(prop + "=" + d[prop])
		}
		return arr.join("&")
	}
	
	return new Promise((resolve,reject)=>{
		let xhr = new XMLHttpRequest()
		let isGet = type.toLowerCase()=="get"
		let sendData = isGet?null:data
		let _url = isGet?`${url}?${params(data)}`:url
		xhr.open(type,_url,true)	//第三个参数为可选，true为异步，false为同步模式。默认true
		xhr.send(sendData)
		xhr.onreadystatechange = () =>{
			if(xhr.readyState==4){
				if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
					resolve(JSON.parse(xhr.responseText))
				}else{
					reject(xhr.status+"请求失败")
				}
			}
		}
	})
}

var Book = {name:"钢铁是怎样炼成的"}

// 劫持
objProxy(Book,"name")

// 闭包封装
function objProxy(obj,key){
    var val = obj[key]
    Object.defineProperty(obj, key, {
        set: function (newVal) {
            val = newVal
            console.log('你取了一个书名叫做' + newVal)
        },
        get: function () {
            return '《' + val + '》'
        }
    })
}

// 测试
console.log(Book); // {}
console.log(Book.name); // "《钢铁是怎样炼成的》"
Book.name = "三国演义";  //你取了一个书名叫做三国演义
console.log(Book.name); // "《三国演义》"

function myInterval(){
	//do something
	setTimeout(myInterval,200)
}
setTimeout(myInterval,200)


setTimeout(function(){
	//do something
	setTimeout(arguments.callee(),200)
},200)


function sleep(delay){
	var nextTime = Date.now() + delay
	while(Date.now() < nextTime){
		return
	}
}


function sleep(delay){
	return new Promise((resolve)=>{
		setTimeout(()=>{
			resolve(true)
		},delay)
	})
}
(async function(){
	await sleep(5000)
	console.log(2)
})()

Array.prototype.distinct = function(){
	var newArr = []
	for(var i=0;i<this.length;i++){
		if(newArr.indexOf(this[i]) < 0){
			newArr.push(this[i])
		}
	}
	return newArr
}


function _toHump(key){
	// 去除首尾的下划线
	var _key = key.replace(/(^_*)|(_*$)/g,"")
	var splitKey = _key.split("_")
	var HumpKey = splitKey.map((item,index)=>{
		if(index>0 && item!=""){
			item = item[0].toUpperCase()+item.slice(1)
		}
		return item
	})
	return HumpKey.join("")
}

function hump2_(key){
	var splitKey = key.split("")
	var HumpKey = splitKey.map((item,index)=>{
		// 判断是否为大写字母
		if(index>0 && item>="A" && item <= "Z"){
			item = "_" + item
		}
		return item
	})
	return HumpKey.join("")
}